package ru.aslanov.schedule.client;

import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.layout.VLayout;
import ru.aslanov.schedule.client.ds.SchedulesDS;
import ru.aslanov.schedule.client.i18n.I18nUtil;

/**
 * Created by IntelliJ IDEA.
 * Created: Oct 15, 2010 3:09:26 PM
 *
 * @author Sergey Aslanov
 */
public class SchedulePropertiesPanel extends VLayout implements Invalidatable {

    private DynamicForm dynamicForm;
    private Criteria criteria;

    public SchedulePropertiesPanel(ScheduleUser scheduleUser) {
        dynamicForm = new DynamicForm();
        dynamicForm.setWidth(600);
        dynamicForm.setNumCols(2);
        dynamicForm.setColWidths("50", "300");
        dynamicForm.setDataSource(SchedulesDS.getInstance(scheduleUser));
        criteria = new Criteria("encodedKey", scheduleUser.getScheduleKey());
        dynamicForm.fetchData(criteria);

        addMember(dynamicForm);

        Button saveButton = new Button(I18nUtil.cnt().saveChanges());
        saveButton.setAutoFit(true);        
        saveButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                dynamicForm.saveData();
            }
        });

        addMember(saveButton);
    }

    @Override
    public void invalidateData() {
        dynamicForm.invalidateCache();
        dynamicForm.fetchData(criteria);
    }
}
